package chapter01;

public class OrderedCollection<E extends Comparable> {

    private E[] data;
    private int size;

    private static final int DEFAULT_CAPACITY = 10;

    public OrderedCollection() {
        this(DEFAULT_CAPACITY);
    }

    public OrderedCollection(int capacity) {
        data = (E[]) new Comparable[capacity];
        size = 0;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public void makeEmpty() {
        data = (E[]) new Comparable[DEFAULT_CAPACITY];
        size = 0;
    }

    public void insert(E e) {
        insert(size, e);
    }

    public void insert(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add faild. Required index >=0 and index< size");
        }
        if (size == data.length) {
            resize(2 * data.length);
        }

        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size ++;
    }

    private void resize(int capacity) {
        E[] newData = (E[])new Comparable[capacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    public void remove(E e) {
        //先查找元素
        int index = find(e);
        //再删除元素
        if (index != -1) {
            remove(index);
        }
    }

    private void remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("Remove failed. Index is illegal!");
        }
        E returnElement = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size --;
        data[size] = null;
    }

    /**
     * 查找数组元素e所在的索引,如果不存在元素e,则返回-1
     * @param e 待查找的元素
     * @return
     */
    private int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    public boolean isPresent(E e) {
        return find(e) != -1;
    }

    public E findMin() {
        if (size == 0) {
            return null;
        }
        int index = 0;
        for (int i = 0; i < size; i++) {
            if (data[i].compareTo(data[index]) < 0) {
                index =i;
            }
        }
        return data[index];
    }

    public E findMax() {
        if (size == 0) {
            return null;
        }
        int index = 0;
        for (int i = 0; i < size; i++) {
            if (data[i].compareTo(data[index]) > 0) {
                index =i;
            }
        }
        return data[index];
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array of the OrderedCollection: size = %d, capacity = %d\n",size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1){
                res.append(",");
            }
        }
        res.append(']');
        return res.toString();
    }
}
